<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="dataForm.password" placeholder="修改密码" show-password></el-input>
      </el-form-item>
      <el-form-item label="用户邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="用户邮箱"></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="dataForm.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="用户年龄" prop="age">
        <el-input v-model="dataForm.age" placeholder="用户年龄"></el-input>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker v-model="dataForm.birthDate" type="date" placeholder="选择日期">
        </el-date-picker>
        <!-- <el-input v-model="dataForm.birthDate" placeholder="用户出生日期"></el-input> -->
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        userId: 0,
        username: "",
        password: "",
        email: "",
        status: "",
        sex: "",
        age: "",
        birthDate: "",
      },
      dataRule: {
        username: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
        password: [{ required: true, message: "不能为空", trigger: "blur" }],
        email: [{ required: true, message: "用户邮箱不能为空", trigger: "blur" }],
        status: [{ required: true, message: "1是启用0是禁用不能为空", trigger: "blur" }],
        sex: [{ required: true, message: "1为男性0为女性不能为空", trigger: "blur" }],
        age: [{ required: true, message: "用户年龄不能为空", trigger: "blur" }],
        birthDate: [{ required: true, message: "用户出生日期不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.userId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.userId) {
          this.$http({
            url: this.$http.adornUrl(`/generator/missuser/info/${this.dataForm.userId}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.username = data.missUser.username;
              this.dataForm.password = data.missUser.password;
              this.dataForm.email = data.missUser.email;
              this.dataForm.status = data.missUser.status;
              this.dataForm.sex = data.missUser.sex;
              this.dataForm.age = data.missUser.age;
              this.dataForm.birthDate = data.missUser.birthDate;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/generator/missuser/${!this.dataForm.userId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              userId: this.dataForm.userId || undefined,
              username: this.dataForm.username,
              password: this.dataForm.password,
              email: this.dataForm.email,
              status: this.dataForm.status,
              sex: this.dataForm.sex,
              age: this.dataForm.age,
              birthDate: this.dataForm.birthDate,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
